#include "timer.h"
#include "init.h"
#include "common.h"

static struct timer_list timer;

static struct timer_config {
	struct init_block block;
	timer_callback_t cb;
} timer_config;

int timer_init(struct init_block *block) {
	struct timer_config *config = container_of(block, struct timer_config, block);
	PR_INFO("setup timer %s", block->name);
	timer_setup(&timer, config->cb, 0);
	return 0;
}

void timer_cleanup(struct init_block *block) {
	PR_INFO("shutdown timer %s", block->name);
	del_timer_sync(&timer);
	PR_INFO("timer %s shutdown", block->name);
	return;
}

static struct timer_config timer_config = {
	.block = {
		.name = "timer1",
		.init_fn = timer_init,
		.cleanup_fn = timer_cleanup,
		.drop_fn = NULL,
	},
	.cb = NULL,
};

int init_timer(timer_callback_t cb)
{
	timer_config.cb = cb;
	PR_INFO("begin init timer %s", timer_config.block.name);
	return try_init(&timer_config.block);
}

int start_timer(unsigned long expires) 
{
	return mod_timer(&timer, expires);
}





